package com.example.qihuibao.widget.dialog

import android.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.*
import com.example.qihuibao.constant.Global
import com.example.qihuibao.enum.ChannelStd
import com.example.qihuibao.enum.MealStatus
import com.example.qihuibao.module.main.MainActivity
import com.example.qihuibao.utils.EnumUtils
import com.example.qihuibao.utils.extension.toast
import com.example.qihuibao.widget.view.CommonRecyclerView
import com.example.qihuibao.widget.view.EdittextWithText

/**
 *@author: cww
 *@desc:企业信息操作
 *@date: 5/16/23 10:58 AM
 */
class MealFeeQueryDialog(val mealId:String) : BaseDialogFragment(R.layout.dialog_meal_fee_query) {
    var modifing = false
    var selectChannelStd: ChannelStd? = null
    //
    val mealFeeList = mutableListOf<Fee>()
    val empTypeList = mutableListOf<ReqAddFeesBean.EmpType>()

    override fun initView() {
        mView?.findViewById<TextView>(R.id.tv_title)?.text = "餐别$mealId 收费标准"
        (activity as MainActivity).getMainViewModel().queryMealInfo(ReqQueryMealInfoBean(Global.canteenId, mealId)).observe(this) {
            if (it.RetCode == "0") {
                if (!it.FeeList.isNullOrEmpty()) {
                    mealFeeList.clear()
                    mealFeeList.addAll(it.FeeList)
                }
                mView?.findViewById<CommonRecyclerView>(R.id.rv_meal_fees)?.init(
                    R.layout.item_text_with_delete,
                    LinearLayoutManager(context, RecyclerView.VERTICAL, false),
                    mealFeeList,
                    {parent, pos ->
                        if (mealFeeList.isNotEmpty()) {
                            parent.findViewById<TextView>(R.id.tv_content).text =
                                "收费标准ID: ${it.FeeList!![pos].FeeID}\n" +
                                        "收费标准名称: ${it.FeeList!![pos].FeeName}"
                        }
                    },
                    {pos ->
                        MealFeeUpdateDialog("01", mealFeeList[pos].FeeID!!).show((activity as MainActivity).supportFragmentManager, "")
                    },
                    {pos, itemView ->
                        itemView.findViewById<TextView>(R.id.tv_delete).visibility = if (itemView.findViewById<TextView>(R.id.tv_delete).visibility == View.VISIBLE) View.GONE else View.VISIBLE
                        itemView.findViewById<TextView>(R.id.tv_delete).setOnClickListener {
                            (activity as MainActivity).getMainViewModel().deleteFees(ReqDeleteFeesBean(Global.canteenId, mealId, mealFeeList[pos].FeeID!!)).observe(this) {fee->
                                if (fee.RetCode == "0") {
                                    initView()
                                }else {
                                    activity?.toast("删除失败")
                                }
                            }
                        }
                    }
                )
            }else {
                activity?.toast("查询失败")
            }
        }
        mView?.findViewById<View>(R.id.iv_close)?.setOnClickListener { dismiss() }
    }

}